//
//  SalesOrder.m
//  POS
//
//  Created by sigma xu on 4/19/13.
//  Copyright (c) 2013 sigma xu. All rights reserved.
//

#import "SalesOrderTVC.h"

@interface SalesOrderTVC ()

@end

@implementation SalesOrderTVC
@synthesize sales_order_list;

#pragma mark -
#pragma mark View lifecycle
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    //sales_order_list = [[NSMutableArray alloc] initWithObjects:@"SO 1",@"SO 2",@"SO 3", nil];
    //sales_order_list= sales_order_list_from_scan;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
//    self.navigationItem.rightBarButtonItem = self.refreshControl;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"refresh"     style:UIBarButtonItemStyleBordered target:self action:@selector(refresh)];
    self.title=@"Sales Order";
    [self load_so_list];
    [super viewDidLoad];
    
}
-(IBAction)refresh{
    [self load_so_list];
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return sales_order_list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SalesOrderCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    if (cell == nil){
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }
    if (indexPath.section == 0) {
        SalesOrderDBO *sodbo = [self.sales_order_list objectAtIndex:indexPath.row];
        cell.textLabel.text = sodbo.so_number;
        //NSDate *date = sodbo.create_date;
        //NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //cell.detailTextLabel.text = [dateFormatter stringFromDate:date];
        cell.detailTextLabel.text = sodbo.create_date;
    }
    
    return cell;
}
-(NSMutableArray *) load_so_list{
    sales_order_list = [[NSMutableArray alloc] init];
    SalesOrderDBO *sodbo = [[SalesOrderDBO alloc] init];
    
    sales_order_list = sodbo.get_all_sales_order;
    return sales_order_list;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
     SalesOrderDBO *sodbo = [self.sales_order_list objectAtIndex:indexPath.row];
     [sodbo delete_sales_order];
     [self.sales_order_list removeObjectAtIndex:indexPath.row];
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
     [self.tableView reloadData];
 }
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"sg_show_so_detail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        SalesOrderDBO *sodbo = [self.sales_order_list objectAtIndex:indexPath.row];
        sodbo = sodbo.get_sales_order_by_so_id;
        SalesOrderDetailTVC *sodtvc=(SalesOrderDetailTVC *)[segue destinationViewController];
        sodtvc.so_id=sodbo.so_id;
        [sodtvc load_so_detail];
        sodtvc.title=sodbo.so_number;
    }
}

@end
